//
//  HomeSetionHeader.m
//  NormalFramework
//
//  Created by beisheng03 on 2018/3/8.
//  Copyright © 2018年 beisheng. All rights reserved.
//

#import "HomeSetionHeader.h"
#import "BaseDisablelightBtn.h"
@interface HomeSetionHeader ()
@property (weak, nonatomic) IBOutlet UIView *bgView;

@property (nonatomic, strong) NSMutableArray *titleButtons;

@property (nonatomic, strong) NSArray<NSString *> *titles;

/** <#note#> */
@property (nonatomic, weak) UIImageView *arrorImageView;

/** 上一次按钮 */
@property (nonatomic, weak) UIButton *selectButton;
/** 下划线 */
@property (nonatomic, weak) UIView *underline;
@end

@implementation HomeSetionHeader

- (NSArray<NSString *> *)titles
{
    if (!_titles) {
        _titles = @[@"人气",@"最新",@"进度",@"总需人次"];
    }
    return _titles;
}

- (NSMutableArray *)titleButtons
{
    if (!_titleButtons) {
        _titleButtons = [NSMutableArray array];
    }
    return _titleButtons;
}

- (void)awakeFromNib{
    [super awakeFromNib];
    
    self.contentView.backgroundColor = [UIColor redColor];
    
    [self setupSubViews];
}

- (void)setupSubViews{
    
    // 添加所有标题
    CGFloat x = 0;
    CGFloat y = 0;
    CGFloat w = KScreenW * 0.25;
    
    CGFloat h = 36;
    
    for (NSUInteger i = 0; i < 4; i++) {
        x = i * w;
        BaseDisablelightBtn *btn = [BaseDisablelightBtn buttonWithType:UIButtonTypeCustom];
        btn.titleLabel.font = MYFONT(14);
        btn.tag = i;
        btn.frame = CGRectMake(x, y, w, h);
        [btn setTitle:self.titles[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchDown];
        
        if (i == 3) {
//            btn.backgroundColor = [UIColor blueColor];
            
            btn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 20);
            
            UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(KScreenW - 19, 14, 8, 11)];
            
            imageView.image = [UIImage imageNamed:@"jt_01_16x22_"];
            
            imageView.contentMode = UIViewContentModeScaleAspectFit;
            
            self.arrorImageView = imageView;
            
            [self.bgView addSubview:imageView];
        }
        [self.bgView addSubview:btn];
        
        // 添加到标题按钮数组
        [self.titleButtons addObject:btn];
        
    }
    
    
    
    /*
     问题:1.按钮文字为黑色 2.标题滚动视图可以滚动
     */
    [self titleClick:self.titleButtons[0]];
    
    [self setupLine];
    
    // 下划线
    [self setupUnderline];
    
    
    
    
}



- (void)setupLine{
    
    // 标题按钮
    //    ZMTabBarButton *firstTitleButton = self.titleScrollView.subviews.firstObject;
    
    UIView *underline = [[UIView alloc] init];
    CGFloat underlineH = 0.5;
    underline.frame = CGRectMake(0, 36 - underlineH, KScreenW, underlineH);
    underline.backgroundColor = colorWithA(75, 75, 75,0.2);
    [self.bgView addSubview:underline];
    
    
}



/**
 *  下划线
 */
- (void)setupUnderline
{
    // 标题按钮
    BaseDisablelightBtn *firstTitleButton = self.bgView.subviews[0];
    
    UIView *underline = [[UIView alloc] init];
    CGFloat underlineH = 2;
    underline.frame = CGRectMake(0, 36 - underlineH, 0, underlineH);
    underline.backgroundColor = [firstTitleButton titleColorForState:UIControlStateSelected];
    [self.bgView addSubview:underline];
    self.underline = underline;
    
    // 默认选中最前面的按钮
    // 切换按钮状态
    firstTitleButton.selected = YES;
    //    self.selectButton = firstTitleButton;
    
//     处理下划线
//    [firstTitleButton.titleLabel sizeToFit];
    self.underline.width = KScreenW * 0.25;
    
    
    
    self.underline.centerX = firstTitleButton.centerX;
}


#pragma mark -点击标题就会调用
- (void)titleClick:(UIButton *)button
{
    if ([button.currentTitle isEqualToString:@"总需人次"]) {
        
        self.arrorImageView.image = [UIImage imageNamed:@"jt_02_16x22_"];
        
    }else{
        
        self.arrorImageView.image = [UIImage imageNamed:@"jt_01_16x22_"];
        
        
    }

    
    [self.delegate btnDidClickWith:button];
    
    // 1.选中按钮
    [self selectBtn:button];

    
   
}

- (void)setSeletIndex:(NSInteger)seletIndex{
    
    
    
}




#pragma mark -选中按钮
- (void)selectBtn:(UIButton *)selectBtn
{
    // 设置按钮颜色
    [_selectButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [selectBtn setTitleColor:themeColor forState:UIControlStateNormal];

    
    // 标题缩放
    _selectButton.transform = CGAffineTransformIdentity;
    selectBtn.transform = CGAffineTransformMakeScale(1.3, 1.3);
    
    _selectButton = selectBtn;
    
    
    [UIView animateWithDuration:0.5 animations:^{
        // 处理下划线
        //        self.underline.width = KScreenW/self.childViewControllers.count;
        self.underline.width = KScreenW * 0.25;
        self.underline.centerX = selectBtn.centerX;
        
    } completion:^(BOOL finished) {
        // 2.需要把对应子控制器的view添加上去
        //        [self setupOneViewControllerView:selectBtn.tag];
    }];
}






- (IBAction)btnDidClick:(UIButton *)sender {
    
    if ([self.delegate respondsToSelector:@selector(btnDidClickWith:)]) {
        [self.delegate btnDidClickWith:sender];
    }
    
}

@end
